<template>
  <!-- 页头 -->
  <div class="page-header">
    <el-page-header :icon="ArrowLeft" @back="handleBackBtn">
      <template #title>
        <span> 返回 </span>
      </template>
      <template #content>
        <span class="text-large font-600 mr-3"> 项目内容 </span>
      </template>
    </el-page-header>
  </div>
  <!-- 分割线 -->
  <el-divider />
  <!-- 内容 -->
  <div class="page-contene">
    <el-form :model="form" label-width="120px">
      <el-form-item label="项目标题">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="项目类目">
        <el-tree-select v-model="form.tags" :data="data" multiple :render-after-expand="false" show-checkbox
          check-strictly check-on-click-node placeholder="标签" :clearable="true" />
        <!-- <el-select v-model="form.region" placeholder="请选择项目类目">
          <el-option label="Zone one" value="shanghai" />
          <el-option label="Zone two" value="beijing" />
        </el-select> -->
      </el-form-item>
      <el-form-item label="是否付款">
        <el-radio-group v-model="form.isPayment">
          <el-radio label="0">免费</el-radio>
          <el-radio label="1">付费</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="项目金额">
        <div style="display:flex;align-items:center;width: 100%;">
          <el-input v-model="form.amount" onkeypress="value=value.replace(/[^\d]/g,'')"
            :disabled="form.isPayment === '0'" />
          <el-tooltip class="box-item" effect="dark" content="网站会收取作者收入的10%作为网站运营费用。" placement="right-start">
            <el-icon>
              <QuestionFilled />
            </el-icon>
          </el-tooltip>
        </div>
      </el-form-item>
      <el-form-item label="项目背景/简介">
        <el-input v-model="form.briefly" type="textarea" />
      </el-form-item>
      <el-form-item label="项目封面">
        <UploadImgCover @updateCover="getCover" :action="upLoadImgCoverUrl"></UploadImgCover>
      </el-form-item>
      <el-form-item label="项目介绍">
        <EditorDiv @updateValue="getMsg" :action="upLoadImgUrl"></EditorDiv>
        <!-- <el-input v-model="form.desc" type="textarea" /> -->
      </el-form-item>
      <el-form-item label="上传项目">
        <UploadFileDiv @updateFiles="getFiles" :action="upLoadFileUrl"></UploadFileDiv>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">发布</el-button>
        <el-button @click="handleBackBtn">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, watch } from "vue"
import { useRoute, useRouter } from 'vue-router'
import { ArrowLeft } from '@element-plus/icons-vue'
import Service from '@/utils/axios'
import { ElMessage, ElMessageBox } from "element-plus";
import "element-plus/es/components/message/style/css"; // 按需引入ElMessage样式失败，手动引入
import "element-plus/es/components/message-box/style/css";
import {
  QuestionFilled
} from '@element-plus/icons-vue'

import EditorDiv from '@/components/QuillEditorDiv/index.vue'
import UploadFileDiv from '../UploadFile/index.vue'
import UploadImgCover from '../UploadImgCover/index.vue'

const route = useRoute()
const router = useRouter()

// 返回上一页
const handleBackBtn = () => {
  router.back();
}

// 表单数据
const form = reactive({
  title: '', //项目标题
  tags: '', // 标签
  isPayment: '', // 是否付费
  amount: 0, // 项目金额
  briefly: '', // 项目简介
  fileCoverList: '', // 项目封面
  desc: '',
  files: '',
})


// 监听是否付费
watch(() => form.isPayment, (newValue, oldValue) => {
  if (String(newValue) === '0') {
    form.amount = 0;
  }
})

// 项目封面上传的后端地址
let upLoadImgCoverUrl = ref(
  'http://127.0.0.1:3000/api/uploadimgPublishProCover'
);
// 图片上传的后端地址
let upLoadImgUrl = ref(
  'http://127.0.0.1:3000/api/uploadimgPublishPro'
);
// 文件上传的后端地址
let upLoadFileUrl = ref(
  'http://127.0.0.1:3000/api/uploadfilePublishPro'
);

// Cover传参
const getCover = (val: any) => {
  form.fileCoverList = val;
}

// 富文本框getValue
const getMsg = (val: any) => {
  form.desc = val;
}

// 上传文件getValue
const getFiles = (files: any) => {
  form.files = files;
}

// 表单提交
const onSubmit = () => {
  let userId = localStorage.getItem('userid');
  console.log('submit!', form);
  Service.post('/bkapi/projectInfo/addProjectinfo', {
    form,
    userId
  })
    .then((res: any) => {
      console.log('res', res)
      if (res.data?.status === 200) {
        ElMessage({
          showClose: true,
          message: '发布成功!',
          type: 'success',
        })
      } else {
        ElMessage({
          showClose: true,
          message: res.data?.data,
          type: 'error',
        })
      }
    });
}

// 项目金额输入框-改变时检测是否输入的数字
const handleChange = () => {
  const numRegExp = /^\d+$/;
  console.log(form.amount)
  console.log(numRegExp.test(String(form.amount)));
  // if(numRegExp.test(String(form.amount))) {
  // }
}

// 标签选项数据
const data = [
  {
    value: '前端',
    label: '前端',
    children: [
      {
        value: 'HTML',
        label: 'HTML',
      },
      {
        value: 'CSS',
        label: 'CSS',
      },
      {
        value: 'JS',
        label: 'JS',
      },
      {
        value: 'Vue',
        label: 'Vue',
      },
    ],
  },
  {
    value: '后端',
    label: '后端',
    children: [
      {
        value: 'Java',
        label: 'Java',
      },
      {
        value: '.Net',
        label: '.Net',
      },
      {
        value: 'Python',
        label: 'Python',
      },
    ],
  },
  {
    value: '微信小程序',
    label: '微信小程序',
  },
  {
    value: 'App',
    label: 'App',
  },
]






</script>

<style lang="less" scoped>
.page-contene {
  margin-top: 20px;
  padding: 0 150px;

  :deep(.el-form-item__content) {
    .el-select {
      width: 50%;
    }
  }
}
</style>

<style>
.disUoloadBtn .el-upload--picture-card {
  display: none;
  /* 上传按钮隐藏 */
}
</style>